package com.zzh.lib.core;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

import androidx.core.content.ContextCompat;

import com.zzh.lib.bar.UltimateBarXManager;
import com.zzh.lib.core.config.LogConfig;
import com.zzh.lib.core.listener.HAppBackgroundListener;
import com.zzh.lib.core.listener.HScanWifiResultCallback;
import com.zzh.lib.core.utils.HNetUtils;
import com.zzh.lib.core.wifi.NetworkWatchCallback;
import com.zzh.lib.core.wifi.ScanWifiResultReceiver;
import com.zzh.lib.manager.HAppManager;
import com.zzh.lib.toast.Toaster;
import com.zzh.lib.toast.config.IToastStrategy;
import com.zzh.lib.toast.config.IToastStyle;

import java.util.List;

public class HLibrary {
    private static HLibrary sInstance;
    private Application mContext;

    private static boolean isDebug;

    public static boolean isDebug() {
        return isDebug;
    }

    public static void setDebug(boolean isDebug) {
        HLibrary.isDebug = isDebug;
    }

    private HLibrary() {
    }

    private HScanWifiResultCallback scanWifiResultCallback;


    public static HLibrary getInstance() {
        if (sInstance == null) {
            synchronized (HLibrary.class) {
                if (sInstance == null) sInstance = new HLibrary();
            }
        }
        return sInstance;
    }

    public Application getContext() {
        if (mContext == null) {
            throw new NullPointerException("application is null, please check invoke init method");
        }
        return mContext;
    }

    /**
     * @return getContext()
     * @see HLibrary#getContext()
     */
    public static Application getAppContext() {
        return getInstance().getContext();
    }


    public synchronized void init(Application app) {
        init(app, null);
    }

    boolean isRegister = false;

    public synchronized void init(Application application, HAppBackgroundListener.Callback callback) {
        init(application, null, null, callback);
    }

    public synchronized void init(Application application, IToastStrategy strategy, IToastStyle<?> style,
                                  HAppBackgroundListener.Callback callback) {
        if (application == null) {
            throw new NullPointerException("application is null, please check invoke init method");
        }

        if (isRegister) {
            return;
        }

        if (mContext == null) {
            mContext = application;
            UltimateBarXManager.Companion.getInstance().context = application;
            HAppManager.getInstance().init(application);
            HAppBackgroundListener.getInstance().init(application).addCallback(callback);
        }

        Toaster.init(strategy, style);
        isRegister = true;
    }

    /**
     * 获取最后一个activity
     *
     * @return
     */
    public static Activity getLastActivity() {
        return HAppManager.getInstance().getLastActivity();
    }

    private boolean isNetworkWatchRegister = false;

    private NetworkWatchCallback mNetworkCallback;

    /**
     * 注册网络监测监听
     */
    public void registerWatchNetwork(NetworkWatchCallback callback) {
        isNetworkWatchRegister = true;
        mNetworkCallback = callback;
        HNetUtils.registerNetworkStatus();
    }

    /**
     * 注销网络状态监听
     */
    public void unRegisterWatchNetwork() {
        isNetworkWatchRegister = false;
        mNetworkCallback = null;
        HNetUtils.unRegisterNetworkCallback();
    }

    public void runScanWifiResultCallback(List<ScanResult> scanResult) {
        if (scanWifiResultCallback != null) {
            scanWifiResultCallback.onScanWifiResult(scanResult);
        }
    }

    public void setScanWifiResultCallback(HScanWifiResultCallback scanWifiResultCallback) {
        this.scanWifiResultCallback = scanWifiResultCallback;
    }

    private ScanWifiResultReceiver scanWifiResultReceiver = null;

    /**
     * 注册接收Wifi结果，注意，不使用时一定要注销。{@link #unregisterScanWifiReceiver(Activity)}
     *
     * @param atx      activity
     * @param callback 扫描结果回调，会回调多次。
     */
    public void registerScanWifiReceiver(Activity atx, HScanWifiResultCallback callback) {
        setScanWifiResultCallback(callback);
        IntentFilter filter = new IntentFilter();
        scanWifiResultReceiver = new ScanWifiResultReceiver();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        ContextCompat.registerReceiver(atx, scanWifiResultReceiver, filter,
                ContextCompat.RECEIVER_NOT_EXPORTED);
    }

    /**
     * 不在接收wifi扫描结果
     *
     * @param atx activity
     */
    public void unregisterScanWifiReceiver(Activity atx) {
        setScanWifiResultCallback(null);
        if (atx != null) {
            if (scanWifiResultReceiver != null) {
                atx.unregisterReceiver(scanWifiResultReceiver);
                scanWifiResultReceiver = null;
            }
        }
    }

    /**
     * 网络状态监听回调
     *
     * @return 网络状态监听实例
     */
    public NetworkWatchCallback getNetworkWatchCallback() {
        return mNetworkCallback;
    }

    /**
     * 判断是否注册了
     *
     * @return
     */
    public boolean isNetworkWatchRegister() {
        return isNetworkWatchRegister;
    }

    private String audioSelection, videoSelection, fileSelection, imageSelection;
    private String[] audioSelectionArgs, videoSelectionArgs, fileSelectionArgs, imageSelectionArgs;

    /**
     * 文件过滤条件
     *
     * @param selection     例如"mime_type =? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=?"
     * @param selectionArgs new String[]{MimeTypeUtils.getMimeType("amr"), MimeTypeUtils.getMimeType("mp3"), MimeTypeUtils.getMimeType("aac"), "audio/mp3", "audio/flac", "audio/m4a"};
     */
    public void registerFileSelectionArgs(String selection, String[] selectionArgs) {
        fileSelection = selection;
        fileSelectionArgs = selectionArgs;
    }

    /**
     * 视频文件过滤条件
     *
     * @param selection     例如"mime_type =? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=?"
     * @param selectionArgs new String[]{MimeTypeUtils.getMimeType("amr"), MimeTypeUtils.getMimeType("mp3"), MimeTypeUtils.getMimeType("aac"), "audio/mp3", "audio/flac", "audio/m4a"};
     */
    public void registerVideoSelectionArgs(String selection, String[] selectionArgs) {
        videoSelection = selection;
        videoSelectionArgs = selectionArgs;
    }

    /**
     * 音频文件过滤条件
     *
     * @param selection     例如"mime_type =? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=?"
     * @param selectionArgs new String[]{MimeTypeUtils.getMimeType("amr"), MimeTypeUtils.getMimeType("mp3"), MimeTypeUtils.getMimeType("aac"), "audio/mp3", "audio/flac", "audio/m4a"};
     */
    public void registerAudioSelectionArgs(String selection, String[] selectionArgs) {
        audioSelection = selection;
        audioSelectionArgs = selectionArgs;
    }

    /**
     * 图片文件过滤条件
     *
     * @param selection     例如"mime_type =? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=?"
     * @param selectionArgs new String[]{MimeTypeUtils.getMimeType("amr"), MimeTypeUtils.getMimeType("mp3"), MimeTypeUtils.getMimeType("aac"), "audio/mp3", "audio/flac", "audio/m4a"};
     */
    public void registerImageSelectionArgs(String selection, String[] selectionArgs) {
        imageSelection = selection;
        imageSelectionArgs = selectionArgs;
    }

    public static String getAudioSelection() {
        return getInstance().audioSelection == null ? "" : getInstance().audioSelection;
    }

    public static String[] getAudioSelectionArgs() {
        return getInstance().audioSelectionArgs;
    }

    public static String getVideoSelection() {
        return getInstance().videoSelection == null ? "" : getInstance().videoSelection;
    }

    public static String[] getVideoSelectionArgs() {
        return getInstance().videoSelectionArgs;
    }

    public static String getFileSelection() {
        return getInstance().fileSelection == null ? "" : getInstance().fileSelection;
    }

    public static String[] getFileSelectionArgs() {
        return getInstance().fileSelectionArgs;
    }

    public static String getImageSelection() {
        return getInstance().imageSelection == null ? "" : getInstance().imageSelection;
    }

    public static String[] getImageSelectionArgs() {
        return getInstance().imageSelectionArgs;
    }

    private static LogConfig logConfig;

    public static void registerLogConfig(LogConfig config) {
        HLibrary.logConfig = config;
    }

    public synchronized static LogConfig getLogConfig() {
        if (logConfig == null) {
            logConfig = new LogConfig(new LogConfig.Builder().build());
        }
        return logConfig;
    }

}
